package algotithm.weekendrace.leetcode238.test1;

/**
 * @author 25068
 * @Date 2021/4/25 10:29
 * @Title 5738. K 进制表示下的各位数字总和
 */
class Solution {

    public static void main(String[] args) {
        Solution solution = new Solution();
        int n = 42;
        int k = 2;
        int base = solution.sumBase(n, k);
        System.out.println(base);
    }

    public int sumBase(int n, int k) {
        int res = count(n,k);
        int m = 0;
        while (res != 0) {
            m += res % 10;
            res = res / 10;
        }
        return m;
    }

    public int count(int n,int k) {
        int res = 0;
        int cnt = 0;
        while (n != 0) {
            int remainder = n % k;
            n /= k;
            res += remainder * Math.pow(10,cnt);
            cnt++;
        }
        return res;
    }

}